package com.server;

import com.google.protobuf.ProtocolStringList;
import com.grpc.HelloProto;
import com.grpc.HelloServiceGrpc;
import io.grpc.stub.StreamObserver;

import java.util.concurrent.TimeUnit;

public class HelloServiceImpl extends HelloServiceGrpc.HelloServiceImplBase {

    /**
     * 双向流rpc
     * @param responseObserver
     * @return
     */
    public StreamObserver<HelloProto.HelloRequest> cs2ss(final StreamObserver<HelloProto.HelloResponse> responseObserver) {
        return new StreamObserver<HelloProto.HelloRequest>() {
            public void onNext(HelloProto.HelloRequest value) {
                System.out.println("接收client value =" + value.getName());
                responseObserver.onNext(HelloProto.HelloResponse.newBuilder().setResult("bi").build());
            }

            public void onError(Throwable t) {

            }

            public void onCompleted() {
                System.out.println("接收到全部客户端消息~");
//                以下也可以支持
//                responseObserver.onNext(HelloProto.HelloResponse.newBuilder().setResult("bi").build());
                responseObserver.onCompleted();
            }
        };
    }

    /**
     * 客户端流式rpc
     * @param responseObserver
     * @return
     */
    public StreamObserver<HelloProto.HelloRequest> cs2s(final StreamObserver<HelloProto.HelloResponse> responseObserver) {

        return new StreamObserver() {
            public void onNext(Object value) {
                System.out.println("接收到客户端消息 value = " + value);
            }

            public void onError(Throwable t) {
                System.out.println("监听到客户端异常消息 value = " + t.getMessage());
            }

            public void onCompleted() {
                System.out.println("接收到客户端全部消息");
                HelloProto.HelloResponse.Builder builder = HelloProto.HelloResponse.newBuilder();
                builder.setResult("客户端流式rpc之服务端响应");
                HelloProto.HelloResponse build = builder.build();

                responseObserver.onNext(build);
                responseObserver.onCompleted();

            }
        };
    }

    /**
     * 服务端流式rpc
     * @param request
     * @param responseObserver
     */
    public void c2ss(HelloProto.HelloRequest request, StreamObserver<HelloProto.HelloResponse> responseObserver) {
        String name = request.getName();
        for (int i = 0; i < 9; i++) {
            HelloProto.HelloResponse.Builder builder = HelloProto.HelloResponse.newBuilder();
            HelloProto.HelloResponse.Builder result = builder.setResult(name + "第 " + i + " 个 服务端流式rpc 响应");
            responseObserver.onNext(result.build());
            try {TimeUnit.SECONDS.sleep(1);}catch (Exception e){e.printStackTrace();}
        }
        responseObserver.onCompleted();

    }

    public void print(HelloProto.UserRequest request, StreamObserver<HelloProto.UserResponse> responseObserver) {
        ProtocolStringList authorList = request.getAuthorList();
        for (String author:authorList
        ) {
            System.out.println("author = " + author);
        }
        HelloProto.UserResponse.Builder builder = HelloProto.UserResponse.newBuilder();
        HelloProto.UserResponse helloResponse = builder.setResult("repeat" + " 的响应数据 ~").build();
        responseObserver.onNext(helloResponse);
        responseObserver.onCompleted();
    }

    public void hello(HelloProto.HelloRequest request, StreamObserver<HelloProto.HelloResponse> responseObserver) {
        String name = request.getName();
        HelloProto.HelloResponse.Builder builder = HelloProto.HelloResponse.newBuilder();
        HelloProto.HelloResponse helloResponse = builder.setResult(name + " 的响应数据 ~").build();
        responseObserver.onNext(helloResponse);
        responseObserver.onCompleted();

    }
}
